package com.zt.rongda.stock.service;

import java.text.SimpleDateFormat;
import java.util.List;

import com.zt.rongda.stock.inter.StockSupplierMapper;
import com.zt.rongda.stock.model.StockSupplier;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockSupplierService extends BaseService {

    @Autowired
    private StockSupplierMapper stockSupplierMapper;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from STOCK_SUPPLIER where C_STATUS != -1 ");

        sqlBuilder.addWhereLike2("SUPPLIER_NAME", reqBean.getInfo("supplierName"));

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
    * 通过id查询
    *
    * @param supplierId
    * @return
    */
    public StockSupplier getById(String supplierId) {
        return stockSupplierMapper.selectByPrimaryKey(supplierId);
    }

    /**
    * 新增
    *
    * @param stockSupplier
    */
    public void add(StockSupplier stockSupplier) {
        stockSupplier.setSupplierId(stringId());// 手动设置Id

        stockSupplier.setMakeTime(new Date());
        stockSupplier.setModifyTime(stockSupplier.getMakeTime());
        stockSupplierMapper.insert(stockSupplier);
    }

    /**
    * 修改
    *
    * @param stockSupplier
    */
    public void update(StockSupplier stockSupplier) {
        String supplierId = stockSupplier.getSupplierId();
        if (StringUtil.isEmpty(supplierId)) {
            throw new BusinessException("获取主键失败");
        }

        stockSupplier.setModifyTime(new Date());
        stockSupplierMapper.updateByPrimaryKeySelective(stockSupplier);
    }

    /**
    * 删除
    *
    * @param supplierId
    */
    public void delete(String supplierId) {
        if (StringUtil.isEmpty(supplierId)) {
            throw new BusinessException("获取主键失败");
        }
        stockSupplierMapper.deleteByPrimaryKey(supplierId);
    }

}